from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time

driver_path = Service(executable_path='./chromedriver.exe')
options = Options()
options.add_experimental_option("detach",True)
browser = webdriver.Chrome(service=driver_path, options=options)

# browser.get('https://www.baidu.com')

# s_input = browser.find_element(By.NAME, 'wd')
# s_input.send_keys("python")
# s_input.send_keys(Keys.ENTER) # 按下回车
# 可以使用多种方式
# By.CSS_SELECTOR  By.ID By.NAME By.XPATH By.CLASS_NAME By.LINK_TEXT
# By.PARTIAL_LINK_TEXT  By.TAG_NAME

# 获取一组元素的例子
# browser.get('https://www.icswb.com/')
# news_lis = browser.find_elements(By.CSS_SELECTOR, '#NewsListContainer li')
# for i in news_lis:
#     print(i.text)


# browser.get('https://www.baidu.com')
# browser.find_element(By.CSS_SELECTOR,'#kw').send_keys('蔡徐坤')
# time.sleep(3)
# # 清除输入框内容
# browser.find_element(By.CSS_SELECTOR,'#kw').clear()

# 以豆瓣首页密码登录为例 内嵌页面 iframe

browser.get('https://www.douban.com/')
login_frame = browser.find_element(By.XPATH, '//div[@class="login"]/iframe')
browser.switch_to.frame(login_frame)
browser.find_element(By.CLASS_NAME, 'account-tab-account').click()
browser.find_element(By.ID,'username').send_keys('cxkNgm')
browser.find_element(By.ID,'password').send_keys('88888888')
browser.find_element(By.CSS_SELECTOR,'body > div.account-main.account-body.login-wrap.login-start.account-anonymous > div.account-tabcon-start > div.account-form > div.account-form-field-submit > a').click()



